﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace SpeedTestServerSetup
{
	public partial class Page4 : WizardPage
	{
		public override event StateChangeEvent ReadyStateChanged;

		public Page4()
		{
			InitializeComponent();
		}

		public override void LeavePage(WizardEvents e)
		{
			//only validate if moving to the next page
			if (e.Direction != WizardDirection.Next) { return; }	

			//validate settings - all must be filled
			if (!ValidateSettings())
			{
				DialogResult res = MessageBox.Show(this, "All settings must be filled in or this feature will not function properly.\nIgnore and Continue?", "Alert", MessageBoxButtons.YesNo);
				if (res == DialogResult.No) { e.Cancel = true; return; }
				//else { return; }
			}

			//F:\Projects\SpeedTestServerSetup\SpeedTestServerSetup\bin\Debug\InstallationFiles\SpeedTestReports
			string configPath = Path.Combine(Application.StartupPath, "InstallationFiles\\SpeedTestReports");
			UpdateWebConfig(Path.Combine(configPath, "Web.config"));
		}

		public bool ValidateSettings()
		{
			if (String.IsNullOrEmpty(txtSMTPHost.Text)) { return false; }
			if (String.IsNullOrEmpty(txtSMTPPort.Text)) { return false; }
			if (String.IsNullOrEmpty(txtSMTPUserName.Text)) { return false; }
			if (String.IsNullOrEmpty(txtSMTPPassword.Text)) { return false; }
			if (String.IsNullOrEmpty(txtDisplayName.Text)) { return false; }
			if (String.IsNullOrEmpty(txtDisplayEmail.Text)) { return false; }

			return true;
		}

		public override void EnterPage(WizardEvents e)
		{
			if (ReadyStateChanged != null) { ReadyStateChanged.Invoke(true); }
		}

		private bool UpdateWebConfig(string filePath)
		{
			string connString = ConnectionString.Build("ServerName", "Port", "UserName", "Password", "Database");

			ConfigFile config = new ConfigFile(filePath);
			config.SetValue("configuration/appSettings/add[@key='SMTPHost']", "value", txtSMTPHost.Text);
			config.SetValue("configuration/appSettings/add[@key='SMTPPort']", "value", txtSMTPPort.Text);
			config.SetValue("configuration/appSettings/add[@key='SMTPUserName']", "value", txtSMTPUserName.Text);
			config.SetValue("configuration/appSettings/add[@key='SMTPPassword']", "value", txtSMTPPassword.Text);
			config.SetValue("configuration/appSettings/add[@key='SMTPUseSSL']", "value", chkUseSSL.Checked.ToString());
			config.SetValue("configuration/appSettings/add[@key='SMTPDisplayName']", "value", txtDisplayName.Text);
			config.SetValue("configuration/appSettings/add[@key='SMTPDisplayAddress']", "value", txtDisplayEmail.Text);

			config.Save();

			return true;
		}

		private void txtSMTPPort_KeyPress(object sender, KeyPressEventArgs e)
		{
			int parsedNumber = 0;
			e.Handled = !Int32.TryParse(e.KeyChar.ToString(), out parsedNumber);
			if (e.KeyChar == (char)Keys.Delete || e.KeyChar == (char)Keys.Back) { e.Handled = false; }
		}
	}
}
